package com.peanut.baby.mvp.search;

import android.app.Activity;

import com.peanut.baby.InitManager;
import com.peanut.baby.http.BaseObserver;
import com.peanut.baby.http.BaseResponse;
import com.peanut.baby.http.BaseScheduler;
import com.peanut.baby.http.RetrofitClient;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.model.QA;
import com.peanut.baby.model.SearchResultBundle;
import com.peanut.baby.model.SearchResultItem;
import com.tencent.mars.xlog.Log;

import java.util.ArrayList;
import java.util.List;

import cn.nekocode.rxlifecycle.RxLifecycle;
import io.reactivex.Observable;


/**
 * Created by anonymouslystudio on 2017/8/14.
 */

public class SearchPresenter implements SearchContract.Presenter{
    public static final int ORG_DISPLAY_SIZE = 5;
    private SearchContract.View view;
    Activity activity;
    public SearchPresenter(SearchContract.View view, Activity activity){
        this.view = view;
        this.activity = activity;
    }


    @Override
    public void praiseQA(final QA qa) {
        Observable obs = RetrofitClient.getInstance().praiseQA(qa.id+"", InitManager.getInstance().getUserId(),InitManager.getInstance().getUser().userToken);
        obs.compose(BaseScheduler.<BaseResponse<QA>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<QA>() {
            @Override
            protected void onHandleSuccess(QA o) {
                qa.praiseFlag = o.praiseFlag;
                qa.praiseCount = o.praiseCount;
                view.onPraiseResult(true,qa,"");
            }

            @Override
            protected void onHandleError(String code,String msg) {
                view.onPraiseResult(false,qa,msg);
            }

        });
    }

    @Override
    public void search(String text) {
        Observable obs = RetrofitClient.getInstance().search(text,InitManager.getInstance().getUserId());
        obs.compose(BaseScheduler.<BaseResponse<SearchResultBundle>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<SearchResultBundle>() {
                    @Override
                    protected void onHandleSuccess(SearchResultBundle o) {
                        view.onSearchResultGet(true,prepareSearchResult(o),o,"");
                    }

                    @Override
                    protected void onHandleError(String code,String error) {
                        view.onSearchResultGet(false,null,null,error);
                    }
                });
    }

    @Override
    public void joinRoom(final LiveRoom room) {
        Observable observable = RetrofitClient.getInstance().enroleLiveRoom(InitManager.getInstance().getUser().userToken,
                InitManager.getInstance().getUserId(),room.roomId+"");
        observable.compose(BaseScheduler.<BaseResponse<String>>compose())
                .compose(RxLifecycle.bind(activity).withObservable())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected void onHandleSuccess(String o) {
                        room.isEnroled = 1;
                        view.onJoinResult(true,room,"");
                    }

                    @Override
                    protected void onHandleError(String code,String msg) {
                        view.onJoinResult(false,room,msg);
                    }

                });
    }

    private List<SearchResultItem> prepareSearchResult(SearchResultBundle bundle){
        List<SearchResultItem> results = new ArrayList<>();

        if(bundle.liveRoomList != null && !bundle.liveRoomList.isEmpty()){
            SearchResultItem liveLabel = new SearchResultItem();
            liveLabel.type = SearchResultItem.ITEM_LABEL;
            liveLabel.data = "直播";

            results.add(liveLabel);

            int size = bundle.liveRoomList.size();
            Log.d("TAG","liveRoomList "+size);
            if(size > ORG_DISPLAY_SIZE){
                size = ORG_DISPLAY_SIZE;
            }

            for(int i=0;i<size;i++){
                SearchResultItem live = new SearchResultItem();
                live.type = SearchResultItem.ITEM_LIVE;
                live.data = bundle.liveRoomList.get(i);

                results.add(live);
            }

            if(bundle.liveRoomList.size() > ORG_DISPLAY_SIZE){
                SearchResultItem liveMore = new SearchResultItem();
                liveMore.type = SearchResultItem.ITEM_LIVE_MORE;
                liveMore.data = "更多直播";

                results.add(liveMore);
            }


        }

        if(bundle.qaList != null && !bundle.qaList.isEmpty()){
            SearchResultItem qaLabel = new SearchResultItem();
            qaLabel.type = SearchResultItem.ITEM_LABEL;
            qaLabel.data = "问答";

            results.add(qaLabel);

            int size = bundle.qaList.size();
            Log.d("TAG","qaList "+size);
            if(size > ORG_DISPLAY_SIZE){
                size = ORG_DISPLAY_SIZE;
            }

            for(int i=0;i<size;i++){
                SearchResultItem qa = new SearchResultItem();
                qa.type = SearchResultItem.ITEM_QA;
                qa.data = bundle.qaList.get(i);

                results.add(qa);
            }

            if(bundle.qaList.size() > ORG_DISPLAY_SIZE){
                SearchResultItem qaMore = new SearchResultItem();
                qaMore.type = SearchResultItem.ITEM_QA_MORE;
                qaMore.data = "更多问答";
                results.add(qaMore);
            }


        }

        if(bundle.circleList != null && !bundle.circleList.isEmpty()){
            SearchResultItem bbsLabel = new SearchResultItem();
            bbsLabel.type = SearchResultItem.ITEM_LABEL;
            bbsLabel.data = "内容";

            results.add(bbsLabel);

            int size = bundle.circleList.size();
            Log.d("TAG","circleList "+size);
            if(size > ORG_DISPLAY_SIZE){
                size = ORG_DISPLAY_SIZE;
            }

            for(int i=0;i<size;i++){
                SearchResultItem bbs = new SearchResultItem();
                bbs.type = SearchResultItem.ITEM_BBS;
                bbs.data = bundle.circleList.get(i);

                results.add(bbs);
            }

            if(bundle.circleList.size() > ORG_DISPLAY_SIZE){
                SearchResultItem bbsMore = new SearchResultItem();
                bbsMore.type = SearchResultItem.ITEM_BBS_MORE;
                bbsMore.data = "更多内容";

                results.add(bbsMore);
            }
        }
        return results;
    }
}
